package com.lyn.view.pieChart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.lyn.view.pieChart.entity.Attributes;

import java.util.List;

/**
 * @author:longyongning
 * @date:2021/10/8
 * @description: 饼图
 */
public class PieChartView extends View {

    private Paint paint;
    private List<Attributes> attributesList;

    public void setAttributesList(List<Attributes> attributesList) {
        this.attributesList = attributesList;
        postInvalidate();
    }

    public PieChartView(Context context) {
        super(context);
        init();
    }

    public PieChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (attributesList == null || attributesList.size() == 0) {
            return;
        }
        int size = canvas.getWidth();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(100);

        RectF rectF = new RectF();
        rectF.left = 50;
        rectF.top = 50;
        rectF.right = size - 50;
        rectF.bottom = size - 50;
        float startAngle = 0;
        for (Attributes attributes : attributesList) {
            paint.setColor(attributes.getColor());
            canvas.drawArc(rectF, startAngle, attributes.getPercentage()*360, false, paint);
            startAngle=startAngle+attributes.getPercentage()*360;
        }
    }


}
